﻿using System;
using System.ComponentModel;
using System.Windows;

namespace CloudClipboardSync.Utils
{
    public static class GuiTools
    {
        static GuiTools()
        {
            IsDesignTime = DesignerProperties.GetIsInDesignMode(new DependencyObject());
        }

        public static bool IsDesignTime { get; private set; }

        public static void RunOnGuiThread(Action action)
        {
            var disp = Application.Current.Dispatcher;
            if (disp.CheckAccess())
                action();
            else
                disp.Invoke(action);
        }

        public static T RunOnGuiThread<T>(Func<T> function)
        {
            var disp = Application.Current.Dispatcher;
            if (disp.CheckAccess())
                return function();
            else
            {
                var result = default(T);
                disp.Invoke((Action)(() => { result = function(); }));
                return result;
            }
        }
    }
}
